package edu.xmu.software.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.xmu.software.dao.ItemDAO;
import edu.xmu.software.pojo.Item;
import edu.xmu.software.util.*;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class PageController extends MultiActionController {
	
	private ItemDAO itemDAO;
	private String filesDirectoryURL;
	
	public void setItemDAO(ItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	
	public void setFilesDirectoryURL(String filesDirectoryURL) {
		this.filesDirectoryURL = filesDirectoryURL;
	}
	
	public ModelAndView read(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int id = ParamUtils.getIntParameter(request, "id", 0);
		Item it = itemDAO.getLatestItemById(id);
		if (it != null) {
			Map<String, Object> retValue = new HashMap<String, Object>();
			retValue.put("item", it);
			retValue.put("fileBase", filesDirectoryURL);
			int type = it.getType();
			if (type == 3) {
				String name = it.getTitle();
				retValue.put("fileType", getFileType(name));
			}
			return new ModelAndView("view_item", retValue);
		} else {
			response.sendRedirect("userRedirect.do?action=error&type=1");
			return null;
		}
	}
	
	public ModelAndView random(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Random rnd = new Random();
		
		List<Item> itemsList = null;
		
		//TODO: random selected scope should be...
		itemsList = itemDAO.getWordItems(0, 200);
		
		int selected  = Math.abs(rnd.nextInt()) % itemsList.size();	
		
		Item it = itemsList.get(selected);
		
		System.out.println("Selected: " + selected);
		
		if (it != null) {
			Map<String, Object> retValue = new HashMap<String, Object>();
			retValue.put("item", it);
			retValue.put("fileBase", filesDirectoryURL);
			return new ModelAndView("view_item", retValue);
		} else {
			response.sendRedirect("userRedirect.do?action=error&type=1");
			return null;
		}
	}
	
	public ModelAndView revision(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int item_id = ParamUtils.getIntParameter(request, "item-id", 0);
		int rev_id = ParamUtils.getIntParameter(request, "rev-id", 0);
		Item it = itemDAO.getItemByIdAndRev(item_id, rev_id);
		if (it != null) {
			Map<String, Object> retValue = new HashMap<String, Object>();
			retValue.put("item", it);
			retValue.put("fileBase", filesDirectoryURL);
			return new ModelAndView("view_item", retValue);
		} else {
			response.sendRedirect("userRedirect.do?action=error&type=1");
			return null;
		}
	}
	
	public ModelAndView history(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int item_id = ParamUtils.getIntParameter(request, "id", 0);
		List<Item> items = itemDAO.getAllRevsById(item_id);
		if (items != null) {
			System.out.println("d");
			Map<String, Object> retValue = new HashMap<String, Object>();
			retValue.put("itemslist", items);
			System.out.println("dd"+items.size());
			retValue.put("item", items.get(items.size()-1));
			System.out.println("ddd");
			retValue.put("fileBase", filesDirectoryURL);
			return new ModelAndView("history", retValue);
		} else {
			response.sendRedirect("userRedirect.do?action=error&type=1");
			return null;
		}
	}
	
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int item_id = ParamUtils.getIntParameter(request, "item-id", 0);
		int rev_id = ParamUtils.getIntParameter(request, "rev-id", 0);
		Item it = itemDAO.getItemByIdAndRev(item_id, rev_id);
		if (it != null) {
			Map<String, Object> retValue = new HashMap<String, Object>();
			retValue.put("item", it);
			return new ModelAndView("edit_item", retValue);
		} else {
			response.sendRedirect("userRedirect.do?action=error&type=1");
			return null;
		}
	}
	
	//The file view
	public ModelAndView file(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int item_id = ParamUtils.getIntParameter(request, "id", 0);
		Item it = itemDAO.getLatestItemById(item_id);
		if (it != null) {
			Map<String, Object> retValue = new HashMap<String, Object>();
			retValue.put("item", it);
			retValue.put("fileBase", filesDirectoryURL);
			return new ModelAndView("file_item", retValue);
		} else {
			response.sendRedirect("userRedirect.do?action=error&type=1");
			return null;
		}
	}
	
	//The index view
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String page = request.getParameter("page");
		if (page == null) page = "index";
		Map<String, Object> retValue = new HashMap<String, Object>();
		if (page.equals("index")) {
			List<Item> new_items = itemDAO.getLatestItems(12);
			retValue.put("new_items", new_items);
			String index_str = itemDAO.getAttributeValue("index");
			Item it = null;
			if (index_str != null) {
				int index_id = Integer.parseInt(index_str);
				it = itemDAO.getLatestItemById(index_id);
			} else {
				it = new_items.get(0);
			}
			retValue.put("item", it);
			List<Item> catelogs = itemDAO.getCatelogItems(0, 12);
			retValue.put("catelogs", catelogs);
			List<Item> files = itemDAO.getFileItems(0, 12);
			retValue.put("files", files);
			return new ModelAndView("index", retValue);
		} else {
			return new ModelAndView("index", retValue); 
		}
	}
	
	private String getFileType(String fileName) {
		if (fileName.endsWith(".jpg") || fileName.endsWith(".gif") || fileName.endsWith(".png") || fileName.endsWith(".bmp"))
			return "image";
		else if (fileName.endsWith(".doc") || fileName.endsWith(".docx") || fileName.endsWith(".wps"))
			return "docs";
		else if (fileName.endsWith(".mp4") || fileName.endsWith(".avi") || fileName.endsWith(".rm") || fileName.endsWith(".rmvb") || fileName.endsWith(".wmv"))
			return "media";
		else if (fileName.endsWith(".pdf"))
			return "docs";
		else if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx"))
			return "excel";
		else if (fileName.endsWith(".ppt") || fileName.endsWith(".pptx"))
			return "ppt";
		else if (fileName.endsWith(".mp3") || fileName.endsWith(".wav"))
			return "sound";
		return "others";
	}
}
